<template>
  <div class="login-container">
  
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
       <el-row>
      <h3 class="title">源城市后台管理系统</h3>
       </el-row>
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input v-model="loginForm.username" name="username" type="text" auto-complete="on" placeholder="账号" />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :type="pwdType"
          v-model="loginForm.password"
          name="password"
          auto-complete="on"
          placeholder="密码"
          @keyup.enter.native="handleLogin" />
          <!-- 查看密码 -->
        <!-- <span class="show-pwd" @click="showPwd">
          <svg-icon icon-class="eye" />
        </span> -->
      </el-form-item>
      <el-row>

      <el-col :span="16">
      <el-form-item prop="code" >
          <span class="svg-container">      
                <svg-icon  icon-class="validCode"  />
        </span>
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
        >
        </el-input>
      </el-form-item>
      </el-col>
        <div class="login-code" >
          <img :src="codeUrl" @click="getCode()"  class="login-code-img"/>
        </div>
        </el-row>
            <el-row>
           <el-col :span="16">
          <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
        </el-col>
            </el-row>
      <el-form-item style="border: 0;">
     
        <el-button :loading="loading" type="primary" style="width:100%; " size="medium" @click.native.prevent="handleLogin">
        登 录
        </el-button>
      </el-form-item>
      
      <div class="tips">
        <span style="margin-right:20px;">username: admin</span>
        <span> password: admin</span>
      </div>
    </el-form>
     <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2023-2023 suyuan All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { isvalidUsername } from '@/utils/validate'
import { getCaptcha } from '@/api/login'
import axios from 'axios';
import { getToken, setToken, removeToken } from '@/utils/auth'

export default {
  name: 'Login',
  data() {
    // const validateUsername = (rule, value, callback) => {
    //   if (!isvalidUsername(value)) {
    //     callback(new Error('请输入正确的用户名'))
    //   } else {
    //     callback()
    //   }
    // }
    const validatePass = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error('密码不能小于5位'))
      } else {
        callback()
      }
    }
    return {
      codeUrl:"",
      loginForm: {
        username: 'admin',
        password: 'admin',
        code:  ""
      },
      loginRules: {
        // username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePass }]
      },
      loading: false,
      pwdType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created(){
       //获取验证码
       this.getCode();
  },
  methods: {
      getCode(){
         getCaptcha().then(res=>{
        //  console.log(res)
         this.codeUrl=res.content.code
     })
      },
    // showPwd() {
    //   if (this.pwdType === 'password') {
    //     this.pwdType = ''
    //   } else {
    //     this.pwdType = 'password'
    //   }
    // },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          
          // this.$store.dispatch('Login', this.loginForm).then(() => {
          //   this.loading = false
          //   this.$router.push({ path: this.redirect || '/' })
          // }).catch(() => {
          //   this.loading = false
          // })

axios({
    url: 'http://localhost:8080/city/admin/login',
    method: 'post',
    data: this.loginForm,
    transformRequest: [function (data) {
    let ret = ''
    for (let it in data) {
      ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
    }
    return ret
  }],
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
  },
}).then(res=>{
  // console.log(res)
  if(res.data.code==200){
    // console.log(res)
     this.$modal.msgSuccess("登录成功");
            this.loading = false;
          setToken(res.data.content.token)
          this.$store.commit('SET_TOKEN', res.data.content.token)
          // localStorage.setItem('token',res.data.content.token);
          // console.log("----")
          // console.log(localStorage.getItem('token'))
          // localStorage.removeItem('token');
          this.$router.push( '/' )
  }else if(res.data.code==411){
      this.$modal.notifyError("用户名或密码错误");
      this.loading = false
       //获取验证码
       this.getCode();
  }else if(res.data.code==410){
          this.$modal.notifyError("验证码错误");
      this.loading = false
       //获取验证码
       this.getCode();
  }
}).catch(() => {});
    
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$bg:#2d3a4b;
$light_gray:#eee;

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 5px;
    width: 89%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #707070;
      height: 0x;
      &:-webkit-autofill {
         box-shadow: 0 0 0px 1000px #f8f9fa inset !important;
        // -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color:#707070 !important;
      }
    }
  }
  .el-form-item {
    border: 1px solid rgba(8, 8, 8, 0.1);
    background: rgba(250, 246, 246, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}

</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;
.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background-color: $bg;
  background: url("~@/assets/images/loginbackground.jpg");
  background-size: cover;
  .login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
    position: absolute;
    left: 0;
    right: 0;
    max-width: 100%;

    margin: 120px auto;
  }
  .tips {
    font-size: 10px;
    color:#bfbfbf;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 2px;
    // display: inline-block;
  }
  .title {
    font-size: 20px;
    font-weight: 100;
    color: #707070;
    margin: 10px auto 20px auto;
    text-align: center;
    font-weight: bold;
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .login-code {
  width: 33%;
  height: 40px;
  float: right;
  // background: #707070;
  img {
    cursor: pointer;
    vertical-align: middle;
    margin: 0px auto auto 2px;
  }
}
  .login-code-img {

    background: #1e6acd;
    height: 40px;
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
}
</style>
